package com.skd.mall.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.skd.common.utils.PageUtils;
import com.skd.common.utils.R;
import com.skd.mall.product.entity.AttrAttrgroupRelationEntity;
import com.skd.mall.product.entity.AttrEntity;
import com.skd.mall.product.entity.AttrGroupEntity;
import com.skd.mall.product.service.AttrGroupService;
import com.skd.mall.product.service.AttrService;
import com.skd.mall.product.service.CategoryService;
import com.skd.mall.product.vo.AttrAttrgroupRelationVo;
import com.skd.mall.product.vo.AttrGroupWithAttrsVo;
import com.skd.mall.product.vo.AttrResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 属性分组
 *
 * @author skadi
 * @email 2328158977@qq.com
 * @date 2024-08-07 14:14:44
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    //http://localhost:8070/app/product/attrgroup/attr/relation/delete
    @RequestMapping("/attr/relation/delete")
    public R deleteManyRelation(@RequestBody List<AttrResponseVo> attrResponseVos){
        attrService.deleteRelation(attrResponseVos);
        return R.ok();
    }

    //product/attrgroup/225/withattr?t=1724206177824
    @RequestMapping("/{catelogId}//withattr")
    private R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId){
        List<AttrGroupWithAttrsVo> list = attrGroupService.getAttrGroupWithAttrs(catelogId);
        return R.ok().put("data",list);
    }

    /*attrgroup/2/attr/relation?t=1723725509088*/
    @RequestMapping("/{attrGroupId}/attr/relation")
    public R getAttrehtitesWithId(
            @RequestParam Map<String, Object> params,
            @PathVariable("attrGroupId") Integer attrGroupId){
        List<AttrEntity> list = attrGroupService.queryAttrehtitesWithId(params,attrGroupId);
        return R.ok().put("list",list);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrGroupService.queryPage(params);
        return R.ok().put("page", page);
    }


    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catelogId){
        PageUtils page = attrGroupService.queryPage(params,catelogId);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @Autowired
    CategoryService categoryService;
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        //根据找到的属性组对应的分类id，找到对应的 一级 二级 三级数据
        Long catelogID = attrGroup.getCatelogId();
        Long[] paths = categoryService.findCatelogPath(catelogID);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    //http://localhost:8070/app/product/attrgroup/3/noattr/relation?t=1724028049793&page=1&limit=10&key=
    @Autowired
    private AttrService attrService;
    @RequestMapping("/{attrGroupId}/noattr/relation")
    public R queryAttr(@RequestParam Map<String, Object> params,@PathVariable("attrGroupId") Integer attrGroupId){
        PageUtils page = attrService.queryAddAttr(params,attrGroupId);
        return R.ok().put("page",page);
    }

    //http://localhost:8070/app/product/attrgroup/attr/relation
    @RequestMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrAttrgroupRelationVo> attrAttrgroupRelationVos){
        attrService.AddNewRelation(attrAttrgroupRelationVos);
        return R.ok();
    }
}
